import type { UserInfo } from '@/types/userTypes'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'userInfo',
  () => {
    const userInfo = ref<UserInfo>()
    const token = ref<string | null>(localStorage.getItem('token'))

    // 设置用户信息
    const setUserInfo = (info: UserInfo) => {
      userInfo.value = info

      // 同时设置token
      token.value = userInfo.value?.token || null
      localStorage.setItem('token', token.value || '')
    }

    return {
      userInfo,
      setUserInfo,
    }
  },

  // 开启持久化存储配置
  {
    persist: true,
  },
)
